package com.k.kppcloud.consumer.listener;

import com.alibaba.cloud.nacos.discovery.NacosServiceDiscovery;
import com.alibaba.nacos.api.common.Constants;
import com.alibaba.nacos.api.naming.listener.Event;
import com.alibaba.nacos.api.naming.listener.EventListener;
import com.alibaba.nacos.api.naming.listener.NamingEvent;
import com.alibaba.nacos.api.naming.pojo.Instance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
import org.springframework.cloud.loadbalancer.cache.LoadBalancerCacheManager;
import org.springframework.cloud.loadbalancer.core.CachingServiceInstanceListSupplier;
import org.springframework.stereotype.Component;

import java.util.List;

//TODO 监听 服务下线，否则loadbalancer还会轮询到下线的服务好几次，才会感知。
@Component
public class MyNacosEventListener implements EventListener {
//    @Autowired
//    private LoadBalancerCacheManager loadBalancerCacheManager;
    @Override
    public void onEvent(Event event) {
        System.out.println("listener-nacos:" + event);
        if (event instanceof NamingEvent) {
            List<Instance> instances = ((NamingEvent) event).getInstances();
            String serviceName = ((NamingEvent) event).getServiceName();
            if (instances.size() == 0) {
                System.out.println(serviceName + "服务下线,服务异常下线赶紧去看看");
                System.out.println("============服务下线" + serviceName);
                System.out.println("============" + instances);
                clearLoadBalancerCache(event);
            } else {
                System.out.println("============服务上线" + serviceName);
                System.out.println("============" + instances);
            }
        }
    }

    private void clearLoadBalancerCache(Event event) {
//        LoadBalancerCacheManager loadBalancerCacheManager = applicationContext.getBean(LoadBalancerCacheManager.class);
//        log.info("bean:{}",loadBalancerCacheManager);
//        ConcurrentMap<String, Cache> cacheMap = (ConcurrentMap<String, Cache>) ReflectUtil.getFieldValue(loadBalancerCacheManager, "cacheMap");
//        for (Map.Entry<String, Cache> stringCacheEntry : cacheMap.entrySet()) {
//            String key = stringCacheEntry.getKey();
//            Cache cache = stringCacheEntry.getValue();
//            log.info("key:{}\tvalue:{}",key,cache);
//            cache.clear();
//        }
//        factory.destroy();
//        Cache cache = loadBalancerCacheManager.getCache(CachingServiceInstanceListSupplier.SERVICE_INSTANCE_CACHE_NAME);
//        if (cache != null) {
//            NamingEvent namingEvent = ((NamingEvent) event);
//            String serviceName = namingEvent.getServiceName();
//            String[] split = serviceName.split(Constants.SERVICE_INFO_SPLITER);
//            String serviceId = split[1];
//            System.out.println("收到更新服务消息："+ serviceId);
//            List<Instance> instances = namingEvent.getInstances();
//            cache.put(serviceId, NacosServiceDiscovery.hostToServiceInstanceList(instances, serviceId));
//        }
    }
}
